package dtos

import "github.com/edgexfoundry/edgex-go/internal/app/models"

//type ProductResourceOperation struct {
//	ProductResourceId string            `json:"productResourceId" yaml:"productResourceId" validate:"required"` // The replacement of Object field
//	DefaultValue      string            `json:"defaultValue" yaml:"defaultValue"`
//	Mappings          map[string]string `json:"mappings" yaml:"mappings"`
//}
//
//type ProductCommand struct {
//	Name string `json:"name" yaml:"name" validate:"required,edgex-dto-none-empty-string,edgex-dto-rfc3986-unreserved-chars"`
//	Code               string                     `json:"code,omitempty"` // 产品功能code
//	IsHidden           bool                       `json:"isHidden" yaml:"isHidden"`
//	ReadWrite          string                     `json:"readWrite" yaml:"readWrite" validate:"required,oneof='R' 'W' 'RW' 'WR'"`
//	ResourceOperations []ProductResourceOperation `json:"resourceOperations" yaml:"resourceOperations" validate:"gt=0,dive"`
//}
//
//type ProductFunction struct {
//	// 产品功能id
//	Id string `json:"id"`
//	// 产品id
//	ProductId string `json:"productId"`
//	// 产品功能command
//	Command ProductCommand `json:"productCommand"`
//}

type ProductResourceOperation struct {
	ProductResource   string            `json:"productResource" yaml:"productResourceId" validate:"required"`   // The replacement of Object field
	ProductResourceId string            `json:"productResourceId" yaml:"productResourceId" validate:"required"` // The replacement of Object field
	DefaultValue      string            `json:"defaultValue" yaml:"defaultValue"`
	Mappings          map[string]string `json:"mappings" yaml:"mappings"`
	Code              string            `json:"code" yaml:"code"`
	ValueType         string            `json:"valueType" yaml:"valueType"`
}

type ProductCommand struct {
	Description        string                     `json:"description"`
	Name               string                     `json:"name" yaml:"name" validate:"required,edgex-dto-none-empty-string,edgex-dto-rfc3986-unreserved-chars"`
	Code               string                     `json:"code,omitempty"` // 产品功能code
	IsHidden           bool                       `json:"isHidden" yaml:"isHidden"`
	ReadWrite          string                     `json:"readWrite" yaml:"readWrite" validate:"required,oneof='R' 'W' 'RW' 'WR'"`
	ResourceOperations []ProductResourceOperation `json:"resourceOperations" yaml:"resourceOperations" validate:"gt=0,dive"`
}

type ProductFunction struct {
	Id        string         `json:"id"`             // 产品功能id
	ProductId string         `json:"productId"`      // 产品id
	Command   ProductCommand `json:"productCommand"` // 产品功能command
}

func toProductResourceOperationModel(dtopro ProductResourceOperation) models.ProductResourceOperation {
	var pro models.ProductResourceOperation
	pro.ProductResource = dtopro.ProductResource
	pro.ProductResourceId = dtopro.ProductResourceId
	pro.DefaultValue = dtopro.DefaultValue
	pro.Mappings = dtopro.Mappings
	return pro
}

func toProductFunctionCommandModel(dtopc ProductCommand) models.ProductCommand {
	var pc models.ProductCommand
	pc.Description = dtopc.Description
	pc.Name = dtopc.Name
	pc.Code = dtopc.Code
	pc.IsHidden = dtopc.IsHidden
	pc.ReadWrite = dtopc.ReadWrite
	for _, v := range dtopc.ResourceOperations {
		pro := toProductResourceOperationModel(v)
		pc.ResourceOperations = append(pc.ResourceOperations, pro)
	}
	return pc
}

func ToProductFunctionModel(dto ProductFunction) models.ProductFunction {
	var pf models.ProductFunction
	pf.Id = dto.Id
	pf.ProductId = dto.ProductId
	pf.Command = toProductFunctionCommandModel(dto.Command)
	return pf
}

func fromProductResourceOperationModelToDto(pro models.ProductResourceOperation) ProductResourceOperation {
	var dtopro ProductResourceOperation
	dtopro.ProductResource = pro.ProductResource
	dtopro.ProductResourceId = pro.ProductResourceId
	dtopro.DefaultValue = pro.DefaultValue
	dtopro.Mappings = pro.Mappings
	dtopro.Code = pro.Code
	dtopro.ValueType = pro.ValueType
	return dtopro
}

func FromProductFunctionCommandModelToDto(pc models.ProductCommand) ProductCommand {
	var dtopc ProductCommand
	dtopc.Description = pc.Description
	dtopc.Name = pc.Name
	dtopc.Code = pc.Code
	dtopc.ReadWrite = pc.ReadWrite
	dtopc.IsHidden = pc.IsHidden
	for _, v := range pc.ResourceOperations {
		dtopro := fromProductResourceOperationModelToDto(v)
		dtopc.ResourceOperations = append(dtopc.ResourceOperations, dtopro)
	}
	return dtopc
}

func FromProductFunctionModelToDto(pf models.ProductFunction) ProductFunction {
	var dto ProductFunction
	dto.Id = pf.Id
	dto.ProductId = pf.ProductId
	dto.Command = FromProductFunctionCommandModelToDto(pf.Command)
	return dto
}
